<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="home-header">
        <img src="../assets/heima.png">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="handleOutClick">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 边栏 -->
      <el-aside :width="this.openMenu ? '64px' : '200px'">
        <div class="aside-button" @click="closeMenuClick">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="this.open"
          :collapse="this.openMenu"
          :collapse-transition="this.openTransition"
          router
          :default-active="this.activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]" ></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + innerItem.path"
              v-for="innerItem in item.children"
              :key="innerItem.id"
              @click="saveNowState('/' + innerItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <span>{{innerItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      open: true,
      openMenu: false,
      openTransition: false,
      // 被激活的地址
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    handleOutClick() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单数据
    async getMenuList () {
      const result = await this.$http.get('menus')
      if (result.data.meta.status !== 200) return this.$message.error(result.data.meta.msg)
      this.menuList = result.data.data
      console.log(result.data)
    },
    // 侧边栏折叠
    closeMenuClick () {
      this.openMenu = !this.openMenu
    },
    // 保存当前激活状态
    saveNowState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-menu{
  border-right: none;
}
.home-container{
  height: 100%;
}
.el-header{
  background-color: #373D41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  font-size: 20px;
  color:#fff;
  .home-header{
    display: flex;
    align-items: center;
    span{
      padding-left: 10px;
    }
  }
}
.el-aside{
  background-color: #333744;
}
.el-main{
  background-color: #EAEDF1;
}
.aside-button{
  background-color: #4a5064;
  color: #fff;
  font-size:24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
